package com.aphrodite.ui.widgets.barcode;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

/**
 * Graphics based outputter to draw barcodes to Graphics objects for printing
 * and display.
 */
class GraphicsOutput extends AbstractOutput {

	private final Graphics g;
	private int savedColour;

	/**
	 * Creates a Graphics2D AbstractOutput
	 * 
	 * @param graphics
	 *            The graphics to output to
	 * @param font
	 *            The font for text rendering
	 * @param fgColor
	 *            Foreground Color
	 * @param bgColor
	 *            Background Color
	 */
	GraphicsOutput(Graphics graphics, Font font, int fgColor, int bgColor) {
		super(font, true, 1.0, fgColor, bgColor);
		this.g = graphics;
	}

	/**
	 * From AbstractOutput - Saves current colour.
	 */
	public void beginDraw() {
		savedColour = g.getColor();
	}

	/**
	 * From AbstractOutput - Draws a bar at the given coordinates onto the
	 * output Graphics.
	 * 
	 * @param x
	 *            the x coordinate
	 * @param y
	 *            the y coordinate
	 * @param width
	 *            the width
	 * @param height
	 *            the height
	 * @param paintWithForegroundColor
	 *            if true, use the foreground color, otherwise use the
	 *            background color
	 */
	public int drawBar(int x, int y, int width, int height, boolean paintWithForegroundColor) {
		if (painting) {
			if (paintWithForegroundColor) {
				g.setColor(this.foregroundColour);
			} else {
				g.setColor(this.backgroundColour);
			}

			g.fillRect((int) (scalar * x), (int) (scalar * y), (int) (scalar * width), (int) (scalar * height));
		}

		return width;
	}

	/**
	 * From AbstractOutput - Restores colour.
	 * 
	 * @param width
	 *            The output width (in pixels) of the barcode
	 * @param height
	 *            The output height (in pixels) of the barcode
	 */
	public void endDraw(int width, int height) {
		g.setColor(savedColour);
	}

	// public int drawText(String text, LabelLayout labelLayout) throws
	// OutputException {
	// if (font == null) {
	// return 0;
	// } else {
	// // TODO implement text layout
	// TextLayout layout = new TextLayout(text, font, g.getFontRenderContext());
	// labelLayout.setTextLayout(layout);
	//
	// if (painting) {
	// g.setColor(backgroundColour);
	// g.fillRect(labelLayout.getBackgroundX(), labelLayout.getBackgroundY(),
	// labelLayout.getBackgroundWidth(), labelLayout.getBackgroundHeight());
	// g.setColor(foregroundColour);
	// layout.draw(g, labelLayout.getTextX(), labelLayout.getTextY());
	// }
	//
	// return labelLayout.getBackgroundHeight();
	// }
	// }

	/**
	 * Paint the background the background colour, based on the height and the
	 * width.
	 * 
	 * @param x
	 *            the x coordinate
	 * @param y
	 *            the y coordinate
	 * @param width
	 *            the width to be painted
	 * @param height
	 *            the height to be painted
	 */
	public void paintBackground(int x, int y, int width, int height) {
		if (!painting) {
			return;
		}
		g.setColor(backgroundColour);
		g.fillRect(x, y, width, height);
	}

}
